package com.zxl.app.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxl.common.mapper.SysBookOrderMapper;
import com.zxl.common.mapper.SysMemberMapper;
import com.zxl.common.model.bo.meber.MemberUpdateBo;
import com.zxl.common.model.pojo.SysBookOrder;
import com.zxl.common.model.pojo.SysMember;
import com.zxl.common.model.vo.member.MemberInfoDetailVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zxw
 * @date 2023/4/1
 */
@Service
public class MemberService {

    @Resource
    private SysMemberMapper sysMemberMapper;
    @Resource
    private SysBookOrderMapper sysBookOrderMapper;

    public MemberInfoDetailVo memberInfo(Integer userId) {
        return sysMemberMapper.selectMemberInfo(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void memberUpdate(MemberUpdateBo memberUpdateBo) {
        SysMember sysMember = new SysMember();
        BeanUtil.copyProperties(memberUpdateBo,sysMember);
        sysMember.setId(memberUpdateBo.getMemberId());
        sysMemberMapper.updateById(sysMember);
        SysBookOrder sysBookOrder = sysBookOrderMapper.selectOne(new QueryWrapper<SysBookOrder>()
                .eq("member_id", sysMember.getId()));
        if (sysBookOrder != null){
            BeanUtil.copyProperties(memberUpdateBo,sysBookOrder);
            sysBookOrderMapper.updateById(sysBookOrder);
        }
    }
}
